// pages/goods-specs/index.js
import {
    ReDistributionMall
} from "../../api/shipping.js"
Component({
    /**
     * 组件的属性列表
     */

    // <!-- kucun="{{Benlen}}" 标注 秒杀  Benlen 清除库存-->
    properties: {
        show: {
            type: Boolean,
            value: false
        },
        order: {
            type: Boolean,
            value: true
        },
        isShowt: {
            type: Boolean,
            value: false
        },
        Benlen: {
            type: Boolean,
            value: false
        },
        kucun: {
            type: Boolean,
            value: false
        },
        btnText: {
            type: String,
            value: ''
        },
        dataObj: {
            type: Object,
            value: {}
        },
        // 规格商品列
        option: {
            type: Array,
            value: []
        },
        disabled: {
            type: Boolean,
            value: false
        },
        isCount: {
            type: Boolean,
            value: true
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        isShow: false,
        activeChild: 0,
        goodsList: [{
            goodsImg: "/images/element_christmas_xpzq@2x.png",
            goodsName: "商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称",
            goodsPrice: "310.00",
            goodsSpecs: "规格规格规格个规格规格规格规格规格规格个规格规格规格",
            goodsSpecsCount: 2
        }, ],
        specsList: ['2.5kg', '3.5kg'],
        active: 0, //当前选中规格
        specsName: "",
        propeprice: '',
        count: 1,
        thumb: '',
        property_price: '',
        propertydata: {}
    },

    observers: {

        'show': function (newVal, oldVal) {
            console.log('==', this.data.active, this.data.dataObj)

            let count = 1
            if (this.data.dataObj) {

                if (this.data.dataObj.property !== undefined && this.data.dataObj.property[this.data.active].child.length > 0 && this.data.dataObj.property[this.data.active].child != undefined) {

                    count = this.data.dataObj.property[this.data.active].child[this.data.activeChild].number
                } else {
                    // count = this.data.dataObj.property[this.data.active].number
                }
                this.setData({
                    count: count
                })
            }

            if (this.data.dataObj && this.data.dataObj.detailType == 7) {
                let dataObj = this.data.dataObj
                this.data.thumb = dataObj.thumb.replace('http://static.chonglaoban.cn', '')
                this.data.property_price = dataObj.price
                dataObj.name = dataObj.goodsname
                dataObj.property_name = dataObj.propertyname

                if (dataObj.has_inventorys == 0) {
                    this.setData({
                        disabled: true,
                        btnText: "已售罄"
                    })
                } else {
                    this.setData({
                        disabled: false,
                        btnText: "加入购物车"
                    })
                }
                this.setData({
                    property_price: this.data.property_price,
                    thumb: this.data.thumb,
                    dataObj: dataObj
                })
            } else {
                if (newVal && this.data.dataObj.type == 0) { // 普通
                    this.printImageInfo();
                    if (this.data.dataObj.property[this.data.active].child[this.data.activeChild] != undefined) {
                        let data = this.data.dataObj.property[this.data.active]
                        if (data.child[this.data.activeChild].inventory == 0) {
                            this.setData({
                                disabled: true,
                                btnText: "已售罄"
                            })
                        } else {

                        }
                        let obj = {
                            id: this.data.dataObj.id,
                            propertyid: this.data.propertydata
                        }
                        this.data.dataObj.propertyid = data.child[this.data.activeChild].id
                        this.data.dataObj.propertyname = data.property_name
                        this.data.dataObj.thumb = data.child[this.data.activeChild].img
                        console.log(this.data.dataObj.thumb)
                        this.setData({
                            thumb: this.data.dataObj.thumb
                        })
                    } else {
                        this.data.propertydata = {
                            productid: this.data.dataObj.propertyid
                        }
                        let obj = {
                            id: this.data.dataObj.id,
                            propertyid: this.data.propertydata
                        }
                    }
                } else if (newVal && this.data.dataObj.type == 1) { //一件代发
                    if (this.data.dataObj.property[this.data.active].child[this.data.activeChild] != undefined) {
                        this.data.propertydata = {
                            productid: this.data.dataObj.property[this.data.active].child[this.data.activeChild].id
                        }
                        let obj = {
                            id: this.data.dataObj.id,
                            propertyid: this.data.propertydata
                        }
                        this.getOneGoodsLists(obj)
                    } else {
                        this.data.propertydata = {
                            productid: this.data.dataObj.propertyid
                        }
                        let obj = {
                            id: this.data.dataObj.id,
                            propertyid: this.data.propertydata
                        }
                        this.getOneGoodsLists(obj)
                    }
                } else if (newVal && this.data.dataObj.type == 2) { //京东
                    this.data.propertydata = {
                        id: this.data.dataObj.property[this.data.active].child[this.data.activeChild].id
                    }
                }
            }

            this.setData({
                isShow: newVal
            })
        },
        'dataObj': function (newVal, oldVal) {
            // console.log('接收到的对象：', newVal,newVal.property[0].child[0].spec);
            // this.data.dataObj.property.forEach(element => {
            //     element.child.forEach(elementj => {
            //         Object.assign(elementj
            //             ,{
            //                 property_price: elementj.spec
            //             })
            //     });
            // });
            let currentIndex = 0
            let activeIndex = 0
            if (this.data.dataObj.property && this.data.dataObj.property.length) {
                this.data.dataObj.property.forEach((item, i) => {
                    item.child.forEach((el, index) => {
                        if (el.id === this.data.dataObj.propertyid) {
                            currentIndex = index
                            activeIndex = i
                        }
                    })
                })
            }
            let numB = ''
            if (this.data.kucun == false) {
                numB = newVal.property && newVal.property[0].child[0] && newVal.property[0].child[0].price ? parseFloat(newVal.property[0].child[0].price) : parseFloat(newVal.property[0].price)
            } else {
                numB = newVal.property && newVal.property[0].child[0] && newVal.property[0].child[0].price ? parseFloat(newVal.sellprice) : parseFloat(newVal.sellprice)
            }
            this.setData({
                active: activeIndex,
                activeChild: currentIndex,
                thumb: newVal.property && newVal.property[0].child[0] && newVal.property[0].child[0].img ? newVal.property[0].child[0].img : newVal.property[0].img,
                property_price: newVal.property && newVal.property[0].child[0] ? newVal.property[0].child[0].spec : '',
                propeprice: numB,
                count: newVal.property && newVal.property[0].child[0] ? newVal.property[0].child[0].number : newVal.property[0].number,
            });
        },

    },
    /**
     * 组件的方法列表
     */
    methods: {
        printImageInfo() {
            let thumbimg = this.data.dataObj.thumb
            console.log(thumbimg)
            if (thumbimg.startsWith("http")) {

                thumbimg = thumbimg.replace("http://static.chonglaoban.cn", "")
            }
            this.setData({
                thumb: thumbimg,
                property_price: this.data.dataObj.real_price
            });
        },

        //获取一件代发购物车商品数量
        getOneGoodsLists(item) {
            ReDistributionMall({
                act: 'oneGoodsInfo',
                id: item.id,
                propertyid: item.propertyid.productid

            }).then(res => {
                if (res.cart_numbers == '0') {
                    res.cart_numbers = 1
                }
                wx.nextTick(() => {
                    this.setData({
                        count: res.cart_numbers || 1
                    })
                })
            });
        },


        /**
         * 选择规格回调函数方法
         * @param {*} e 
         */
        // handleSelSpecs(e) {
        //     console.log("diyiggeguige", e.currentTarget.dataset)
        //     const {
        //         index,
        //         value
        //     } = e.currentTarget.dataset
        //     this.setData({
        //         active: index,
        //         specsName: value
        //     })
        // },
        handleSelSpecs(e) {

            console.log(this.data.dataObj, this.data.activeChild)

            const index = e.currentTarget.dataset.index;
            console.log('切换', e, this.data.dataObj.property, )

            this.data.dataObj.propertyid = this.data.dataObj.property[index].child.length > 0 ? this.data.dataObj.property[index].child[this.data.activeChild].id : this.data.dataObj.property[index].id

            this.data.dataObj.propertyname = e.currentTarget.dataset.value.property_name

            let data = this.data.dataObj.property[index]
            if (data.child.length > 0) {
                if (data.child[this.data.activeChild].inventory == 0) {

                    this.setData({
                        disabled: true,
                        btnText: "已售罄"
                    })
                } else {
                    this.setData({
                        disabled: false,
                        btnText: "加入购物车"
                    })
                }
            } else {
                if (data.inventory == 0) {

                    this.setData({
                        disabled: true,
                        btnText: "已售罄"
                    })
                } else {
                    this.setData({
                        disabled: false,
                        btnText: "加入购物车"
                    })
                }
            }



            // propeprice:newVal.property && newVal.property[0].child[0] && newVal.property[0].child[0].price ? newVal.property[0].child[0].price : newVal.property[0].price,
            // count: newVal.property && newVal.property[0].child[0] ? newVal.property[0].child[0].number : '',
            //    thumb: this.data.dataObj.property[index].child[this.data.activeChild].img,
            //   property_price:this.data.dataObj.property[index].child[this.data.activeChild].spec,
            // count: this.data.dataObj.property[index].child[this.data.activeChild].number,
            this.setData({
                active: index,
                activeChild: 0, // 重置第二个规格栏的选中状态
                thumb: this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] && this.data.dataObj.property[index].child[this.data.activeChild].img ? this.data.dataObj.property[index].child[this.data.activeChild].img : this.data.dataObj.property[index].img,

                property_price: this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] ? this.data.dataObj.property[index].child[this.data.activeChild].spec : this.data.dataObj.property[index].spec,

                count: this.data.dataObj.property && this.data.dataObj.property[index].child[this.data.activeChild] ? this.data.dataObj.property[index].child[this.data.activeChild].number : this.data.dataObj.property[index].number,


            });
        },
        handleSelSpecsChild(e) {
            const index = e.currentTarget.dataset.index;
            console.log('e', e, this.data.dataObj.property[this.data.active].child[index].price, this.data.dataObj.property[this.data.active].child[index].number)
            this.setData({
                activeChild: index,
                property_price: this.data.dataObj.property[this.data.active].child[index].price,
                propeprice: this.data.dataObj.property[this.data.active].child[index].price,
                count: this.data.dataObj.property[this.data.active].child[index].number,

            });
            this.data.propertydata = {
                ...e.currentTarget.dataset.value
            }
        },
        /**
         * 商品数量变化回调函数方法
         * @param {*} e 
         */
        handleNumChange(e) {
            this.setData({
                count: e.detail
            })
        },
        /**
         * 确认按钮回调函数方法
         */
        onConfirm(e) {
            console.log('歘', e)
            let num
            // tips = 0商品列表 tips = 1 一件代发 
            if (this.data.dataObj.type == 1) {
                num = 1
            } else {
                num = 0
            }
            console.log('点击', this.data.count)
            this.triggerEvent('confirm', {
                index: this.data.active,
                goodsid: this.data.dataObj[0].goodsid,
                has_inventorys: this.data.dataObj[0].has_inventorys,
                id: this.data.dataObj[0].id,
                marketprice: this.data.dataObj[0].marketprice,
                price: this.data.dataObj[0].price,
                property_price: this.data.dataObj[0].property_price,
                propertyid: this.data.dataObj[0].propertyid,
                thumb: this.data.dataObj[0].thumb,
                type: 'unconsume_region_detail',
                count: this.data.count || 1,

            })
        },
        /**
         * 关闭
         */
        close() {
            this.triggerEvent('close')
        }
    }
})